package com.cj.library.widget.view.loadingview;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.cj.library.widget.R;
import com.cj.library.widget.utiles.DensityUtils;

/**
 * Created by peter on 2016/9/19.
 */
public class CircleLoadingView extends View {

    private Paint paint;
    private float lineLength = 20;
    private float lineWidth = 5;
    private int lineCount = 12;
    private int lineColor = Color.WHITE;
    private int duration = 2500;

    private ObjectAnimator animator;

    public CircleLoadingView(Context context) {
        this(context, null);
    }

    public CircleLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleLoadingView);

            lineWidth = a.getDimension(R.styleable.CircleLoadingView_circle_loading_lineWidth, DensityUtils.dip2px(context, 5));
            lineLength = a.getDimension(R.styleable.CircleLoadingView_circle_loading_lineLength, DensityUtils.dip2px(context, 20));
            lineCount = a.getInt(R.styleable.CircleLoadingView_circle_loading_lineCount, 12);
            lineColor = a.getColor(R.styleable.CircleLoadingView_circle_loading_lineColor, Color.WHITE);
            duration = a.getInt(R.styleable.CircleLoadingView_circle_loading_rotate_duration, 2500);

            a.recycle();
        }

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(lineColor);
        paint.setStrokeWidth(lineWidth);

        animator = ObjectAnimator.ofFloat(this, "rotation", 0, 360);
        animator.setDuration(duration);

        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);

        animator.setInterpolator(new LinearInterpolator());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.drawColor(0x00000000);

        int w = getMeasuredWidth();
        int h = getMeasuredHeight();

        float x = w / 2;
        float cellAngle = 360.0f / lineCount;

        canvas.save();

        for (int i = 0; i < lineCount; i++) {
            int alpha = (int) (255.0f * (i + 1) / lineCount);
//            if(alpha > 220){
//                alpha = 200;
//            }
//            if(alpha < 50){
//                alpha = 50;
//            }
            paint.setAlpha(alpha);
            canvas.drawLine(x, 0, x, lineLength, paint);
            canvas.rotate(cellAngle, w / 2, h / 2);
        }

        canvas.restore();

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        animator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        clearAnimation();
    }
}
